package com.zhanghu.backendZray.manager.property;

import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import com.zhanghu.backendZray.annotation.LoginNoNeed;
import com.zhanghu.backendZray.properties.AlipayProperty;
import com.zhanghu.backendZray.properties.MinIOProperty;
import com.zhanghu.backendZray.properties.WeatherProperty;
import com.zhanghu.backendZray.properties.WxProperty;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author ZhangDong
 * @date 2024/8/8 17:03
 */
@Component
@RequiredArgsConstructor
@Order(1)
public class PropertyManager implements CommandLineRunner {
    private final AlipayProperty alipayProperty;
    private final WxProperty wxProperty;
    private final MinIOProperty minIOProperty;
    private final WeatherProperty weatherProperty;

    // 私钥，解密用
    private String privateKey;

    // 微信clientId
    @SneakyThrows
    public String getWxClientId() {
        return CryptoUtils.decrypt(privateKey, wxProperty.getClientId());
    }

    // 微信clientSecret
    @SneakyThrows
    public String getWxClientSecret() {
        return CryptoUtils.decrypt(privateKey, wxProperty.getClientSecret());
    }

    // 微信登录回调地址
    @SneakyThrows
    public String getWxNotifyUrl() {
        return wxProperty.getNotifyUrl();
    }

    // 微信登录成功跳转的首页地址
    public String getCallBackRedirectIndexUrl() {
        return wxProperty.getCallBackRedirectIndexUrl();
    }

    // alipay 回调地址
    @SneakyThrows
    public String getAlipayNotifyUrl() {
        return alipayProperty.getNotifyUrl();
    }

    // alipay AppId
    @SneakyThrows
    public String getAlipayAppId() {
        return CryptoUtils.decrypt(privateKey, alipayProperty.getAppId());
    }

    // alipay 自己生成的应用私钥
    // 加密报错：Data must not be longer than 53 bytes， 于是只能不加密
    @SneakyThrows
    public String getAlipayPrivateKey() {
        return alipayProperty.getPrivateKey();
    }

    // alipay 支付宝提供的支付宝公钥
    // 加密报错：Data must not be longer than 53 bytes， 于是只能不加密
    @SneakyThrows
    public String getAlipayPublicKey() {
        return alipayProperty.getPublicKey();
    }

    // 获取minIO endpoint
    public String getMinIOEndpoint() {return minIOProperty.getEndpoint();}

    // 获取minIO accessKey
    @SneakyThrows
    public String getMinIOAccessKey() {return CryptoUtils.decrypt(privateKey, minIOProperty.getAccessKey());}

    // 获取minIO secretKey
    @SneakyThrows
    public String getMinIOSecretKey() {return CryptoUtils.decrypt(privateKey, minIOProperty.getSecretKey());}

    // 获取minIO bucketName
    @SneakyThrows
    public String getMinIOBucketName() {return CryptoUtils.decrypt(privateKey, minIOProperty.getBucketName());}

    // 获取minIO 文件链接过期，每次处理的文件条数
    @SneakyThrows
    public String getExpireHandleCount() {return minIOProperty.getExpireHandleCount();}

    @SneakyThrows
    public String getWeatherApiKey() { return CryptoUtils.decrypt(privateKey, weatherProperty.getApiKey());}

    @Override
    @LoginNoNeed
    public void run(String... args) {
        for (String arg : args) {
            if (arg.startsWith("--publicKey=")) {
                privateKey = arg.substring("--publicKey=".length());
                return;
            }
        }
    }
}
